package top.huntwolf.admin.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.common.enums.CodeMsg;
import top.huntwolf.admin.common.enums.OrderStatus;
import top.huntwolf.admin.pojo.orderEntity.Order;
import top.huntwolf.admin.pojo.orderEntity.OrderStatusVo;
import top.huntwolf.admin.pojo.orderEntity.OrderVo;
import top.huntwolf.admin.pojo.wxPay.WxPayRefundOrder;
import top.huntwolf.admin.pojo.wxPay.WxPayRefundResult;
import top.huntwolf.admin.service.CMSOrderService;
import top.huntwolf.admin.service.OrderService;
import top.huntwolf.admin.service.WxPayService;


@Api(description = "cms订单控制器")
@RestController
@CrossOrigin
@RequestMapping(value = "/cms/order")
public class CMSOrderController {

    @Autowired
    private CMSOrderService cmsOrderService;

    @Autowired
    private WxPayService wxPayService;

    @RequiresPermissions(value = "order:find")
    @GetMapping("/allOrder/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiOperation("查询所有订单")
    public JsonResult<PageInfo<Order>> SelectAllOrder(@PathVariable int pageNum, @PathVariable int pageSize){
        return JsonResult.success(cmsOrderService.selectAllOrder(pageNum,pageSize));
    }

    @RequiresPermissions(value = "order:modify:status")
    @PutMapping("/status")
    @ApiOperation("修改订单状态，1为待发货，2为待收货，3为退款,8为已完成,9为取消订单,10为未完成")
    public JsonResult updateOrder(@RequestBody OrderStatusVo order){
        cmsOrderService.updateOrders(order.getOrderId(),order.getOrderStatus());
        return JsonResult.success(null);
    }

    @RequiresPermissions(value = "order:modify:remark")
    @PutMapping("/remark")
    @ApiOperation("增加备忘信息")
    public JsonResult addRemark(@RequestBody Order order){
        cmsOrderService.addRemark(order);
        return JsonResult.success(null);
    }

    @RequiresPermissions(value = "order:refund")
    @PostMapping("/refund")
    @ApiOperation("退款接口")
    public JsonResult refundWxPay(@RequestBody WxPayRefundOrder order){

        wxPayService.wxPayRefund(order);
        return JsonResult.success(CodeMsg.SUCCESS);
    }

    @RequiresPermissions(value = "order:find")
    @GetMapping("/refund/order-id/{orderId}")
    @ApiOperation("查询单笔退款")
    public JsonResult<WxPayRefundResult> queryRefund(@PathVariable String orderId){
        return JsonResult.success(wxPayService.getWxPayRefund(orderId));
    }

    @RequiresPermissions(value = "order:find")
    @GetMapping("/status/{status}/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiOperation("根据状态获取订单")
    public JsonResult<PageInfo<Order>> queryOrderByStatus(@PathVariable OrderStatus status,@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        return JsonResult.success(cmsOrderService.getOrderByStatus(status,pageNum,pageSize));
    }

}
